package com.morizhang.merchanmanager.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.bean.MerChanManagerException;
import com.morizhang.merchanmanager.api.model.JobModel;
import com.morizhang.merchanmanager.api.model.WarehouseShelfModel;
import com.morizhang.merchanmanager.api.plugins.BasicJobManage;
import com.morizhang.merchanmanager.api.plugins.BasicWareManage;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;
import java.util.List;

@ServiceAnnotation(name = "JobService")
public class JobService implements IService {
    @Autowired
    private BasicJobManage basicJobManage;
    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        String callMethodName = requestParams.getString("requestMethod");
        Method callMethod = this.getClass().getMethod(callMethodName, JSONObject.class);
        ApiResult result = (ApiResult)callMethod.invoke(this, requestParams);
        result.setSuccess(true);
        return result;
    }

    public ApiResult assignJob(JSONObject requestParams) throws Exception {
        JobModel jobModel = JSON.toJavaObject(requestParams.getJSONObject("data"), JobModel.class);
        basicJobManage.assignJob(jobModel);
        return new ApiResult();
    }

    public ApiResult queryJobList(JSONObject requestParams){
        JSONObject requestData = requestParams.getJSONObject("data");
        List<JobModel> jobList = basicJobManage.getJobList(requestData.getString("userID"));
        ApiResult result = new ApiResult();
        result.setData(jobList);
        return result;
    }

    public ApiResult completeJob(JSONObject requestParams) throws MerChanManagerException {
        JSONObject requestData = requestParams.getJSONObject("data");
        String userID = requestData.getString("userID");
        String serialNo = requestData.getString("serialNo");
        basicJobManage.checkCompleteAuthority(userID, serialNo);
        basicJobManage.completeJob(serialNo);
        return new ApiResult();
    }
}
